package com.zlx.sharelive.activity;

import android.content.Intent;
import android.view.View;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.zlx.module_base.base_ac.BaseAc;
import com.zlx.module_base.base_api.res_data.VideoChannel;
import com.zlx.module_base.base_api.util.ApiUtil;
import com.zlx.module_base.base_util.SPUtil;
import com.zlx.module_network.api1.livedata.BaseObserver;
import com.zlx.module_network.api1.livedata.BaseObserverCallBack;
import com.zlx.module_network.bean.ApiResponse;
import com.zlx.sharelive.R;
import com.zlx.sharelive.adapter.CommonRecyclerViewAdapter;
import com.zlx.sharelive.adapter.CommonRecyclerViewHolder;
import com.zlx.sharelive.utils.GlideImgManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 频道列表
 */
public class FrequencyListAc extends BaseAc {

    @BindView(R.id.rv_data)
    RecyclerView rv_data;

    private List<VideoChannel.InfoDTO> mList = new ArrayList<>();
    private CommonRecyclerViewAdapter<VideoChannel.InfoDTO> mAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.ac_frequency_list;
    }

    @Override
    public void initViews() {
        super.initViews();

        initRecyclerView();
        getVideoChannel();
    }

    private void initRecyclerView() {
        rv_data.setLayoutManager(new GridLayoutManager(this, 4));

        mAdapter = new CommonRecyclerViewAdapter<VideoChannel.InfoDTO>(this, mList) {
            @Override
            public void convert(CommonRecyclerViewHolder holder, VideoChannel.InfoDTO entity, int position) {
                GlideImgManager.glideLoader(FrequencyListAc.this, entity.getImg(), holder.getView(R.id.iv_image), 1);
                holder.setText(R.id.tv_text, entity.getName());
            }

            @Override
            public int getLayoutViewId(int viewType) {
                return R.layout.item_frequency_list;
            }
        };

        rv_data.setAdapter(mAdapter);

        mAdapter.setOnRecyclerViewItemClickListener(new CommonRecyclerViewAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
                startActivity(new Intent(FrequencyListAc.this, FrequencyAc.class)
                        .putExtra("id", mList.get(position).getId())
                        .putExtra("title", mList.get(position).getName()));
            }
        });
    }

    private void getVideoChannel() {
        ApiUtil.getVideoApi().getVideoChannel(SPUtil.getToken(this)).observe(this, new BaseObserver<>(new BaseObserverCallBack<ApiResponse<VideoChannel>>() {
            @Override
            public void onSuccess(ApiResponse<VideoChannel> data) {
                mList.clear();
                if (null != data) {
                    if (null != data.getData()) {
                        if (null != data.getData().getInfo()) {
                            mList.addAll(data.getData().getInfo());
                        }
                    }
                }
                mAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFinish() {
                super.onFinish();

            }
        }));
    }

    @OnClick(R.id.iv_back)
    public void setOnClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
        }
    }
}
